﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TreeMap
{
	public partial class GUIMainForm : Form
	{
		List<string> OrderedCategories = new List<string>();

		public GUIMainForm()
		{
			InitializeComponent();

			Graphics.GraphicsNode MyNode = new TreeMap.Graphics.GraphicsNode("Willkommen", pCanvas);
			MyNode.TopLeft = new Point(pCanvas.Width / 2 - 50, pCanvas.Height / 2 - 50);
			MyNode.BottomRight = new Point(pCanvas.Width / 2 + 50, pCanvas.Height / 2 + 50);
			MyNode.Draw();
		}

        // Put a Category one level up in list
        private void ButtonUp_Click(object sender, EventArgs e)
        {
            object temp;
            int index;

            if (this.ListBoxCategory.SelectedIndex != 0 && this.ListBoxCategory.SelectedIndex != -1)
            {
                index = this.ListBoxCategory.SelectedIndex;
                temp = this.ListBoxCategory.SelectedItem;

                this.ListBoxCategory.SelectedItem = this.ListBoxCategory.Items[this.ListBoxCategory.SelectedIndex - 1];
                this.ListBoxCategory.SelectedIndex = index;

                this.ListBoxCategory.Items.Remove(this.ListBoxCategory.Items[this.ListBoxCategory.SelectedIndex]);
                this.ListBoxCategory.Items.Insert(index - 1, temp);
                this.ListBoxCategory.SelectedIndex = index-1;
            }
        }

        // Put a category one level down in list
        private void ButtonDown_Click(object sender, EventArgs e)
        {
            object temp;
            int index;

            if (this.ListBoxCategory.SelectedIndex != this.ListBoxCategory.Items.Count-1 && 
                    this.ListBoxCategory.SelectedIndex != -1)
            {
                index = this.ListBoxCategory.SelectedIndex;
                temp = this.ListBoxCategory.SelectedItem;

                this.ListBoxCategory.SelectedItem = this.ListBoxCategory.Items[this.ListBoxCategory.SelectedIndex + 1];
                this.ListBoxCategory.SelectedIndex = index;

                this.ListBoxCategory.Items.Remove(this.ListBoxCategory.Items[this.ListBoxCategory.SelectedIndex]);
                this.ListBoxCategory.Items.Insert(index + 1, temp);
                this.ListBoxCategory.SelectedIndex = index + 1;
            }

        }

        // Load file...
        private void ButtonLoadFile_Click(object sender, EventArgs e)
        {
            if (this.ofdImport.ShowDialog() == DialogResult.OK)
            {
                this.ListBoxCategory.Items.Clear();
                EntryManager.Instance.LoadFile(this.ofdImport.FileName);

                foreach (string cat in EntryManager.Instance.Categories)
                {
                    this.ListBoxCategory.Items.Add(cat);
                }
            }

            this.TrackBarGranularity.Maximum = this.ListBoxCategory.Items.Count;
            this.LabelMaxGranularity.Text = this.ListBoxCategory.Items.Count.ToString();

        }

        private void TrackBarGranularity_ValueChanged(object sender, EventArgs e)
        {
            this.LabelCurrentGranularity.Text = this.TrackBarGranularity.Value.ToString();
            
        }

        /// <summary>
        /// Refresh button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRefresh_Click(object sender, EventArgs e)
        {
			//Order the categories tough to listbox
			OrderedCategories.Clear();
			OrderedCategories.AddRange(this.ListBoxCategory.Items.Cast<string>());
			EntryManager.Instance.OrderEntries(OrderedCategories);
			
			//Setting granularity
			Graphics.GraphicsManager.GetInstance.Granularity = this.TrackBarGranularity.Value;

			//Setting canvas
			Graphics.GraphicsManager.GetInstance.Canvas = pCanvas;

			//Calling the Draw Method in the GraphicsManager
			Graphics.GraphicsManager.GetInstance.Draw(pCanvas);
        }
	}
}
